package com.nav.backend.service.impl;

import com.nav.backend.mapper.UserMapper;
import com.nav.backend.pojo.User;
import com.nav.backend.pojo.UserAddress;
import com.nav.backend.service.UserService;
import org.springframework.stereotype.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 * UserServiceImpl 类实现了 UserService 接口，提供了用户相关的业务逻辑操作
 *
 * @author Seamher
 */
@Service
public class UserServiceImpl implements UserService {

    private final UserMapper userMapper;
    private static final Logger logger = LoggerFactory.getLogger(UserServiceImpl.class);

    public UserServiceImpl(UserMapper userMapper) {
        this.userMapper = userMapper;
    }

    /**
     * 根据用户名查找用户
     *
     * @param account 用户名
     * @return User 对象
     */
    @Override
    public User findByUserName(String account) {
        logger.info("Finding user by account: {}", account);
        return userMapper.findByUserName(account);
    }

    /**
     * 根据邮箱查找用户
     *
     * @param email 用户邮箱
     * @return User 对象
     */
    @Override
    public User findByEmail(String email) {
        logger.info("Finding user by email: {}", email);
        return userMapper.findByUserEmail(email);
    }

    /**
     * 根据手机号查找用户
     *
     * @param phone 用户手机号
     * @return User 对象
     */
    @Override
    public User findByPhone(String phone) {
        logger.info("Finding user by phone: {}", phone);
        return userMapper.findByUserPhone(phone);
    }

    /**
     * 注册新用户
     *
     * @param account  用户名
     * @param password 密码
     */
    @Override
    public void register(String account, String password) {
        logger.info("Registering new user with account: {}", account);
        userMapper.add(account, password);
    }

    /**
     * 注销用户
     *
     * @param account 用户名
     */
    @Override
    public void logout(String account) {
        logger.info("Logging out user with account: {}", account);
        userMapper.logout(account);
    }

    /**
     * 修改用户信息
     *
     * @param account    用户名
     * @param password   密码
     * @param phone      电话
     * @param email      邮箱
     * @param newAccount 新用户名
     */
    @Override
    public void modifyInfo(String account, String password, String phone, String email, String newAccount) {
        logger.info("Modifying info for user with account: {}", account);
        userMapper.modifyInfo(account, password, phone, email, newAccount);
    }

    /**
     * 添加用户地址
     *
     * @param userComment    地址评论
     * @param userAddress    地址
     * @param userAddressX   地址 X 坐标
     * @param userAddressY   地址 Y 坐标
     * @param userAddressInfo 地址信息
     * @param account        用户名
     */
    @Override
    public void addAddress(String userComment, String userAddress, Double userAddressX, Double userAddressY, String userAddressInfo, String account) {
        logger.info("Adding address for user with account: {}", account);
        userMapper.addAddress(userComment, userAddress, userAddressX, userAddressY, userAddressInfo, account);
    }

    /**
     * 获取用户地址列表
     *
     * @param account 用户名
     * @return 用户地址列表
     */
    @Override
    public List<UserAddress> getAddress(String account) {
        logger.info("Getting addresses for user with account: {}", account);
        return userMapper.getAddress(account);
    }

    /**
     * 修改用户地址
     *
     * @param userComment    地址评论
     * @param userAddress    地址
     * @param userAddressX   地址 X 坐标
     * @param userAddressY   地址 Y 坐标
     * @param userAddressInfo 地址信息
     * @param userAddressID  地址 ID
     */
    @Override
    public void modifyAddress(String userComment, String userAddress, Double userAddressX, Double userAddressY, String userAddressInfo, Integer userAddressID) {
        logger.info("Modifying address with ID: {}", userAddressID);
        userMapper.modifyAddress(userComment, userAddress, userAddressX, userAddressY, userAddressInfo, userAddressID);
    }

    /**
     * 删除用户地址
     *
     * @param userAddressId 地址 ID
     */
    @Override
    public void deleteAddress(Integer userAddressId) {
        logger.info("Deleting address with ID: {}", userAddressId);
        userMapper.deleteAddress(userAddressId);
    }

    @Override
    public void logoutUserAddress(String account) {
        userMapper.logoutUserAddress(account);
    }

    @Override
    public void logoutCollectionAddress(String account) {
        userMapper.logoutCollectionAddress(account);
    }

    @Override
    public void logoutCollectionPath(String account) {
        userMapper.logoutCollectionPath(account);
    }

    @Override
    public void logoutRingSearch(String account) {
        userMapper.logoutRingSearch(account);
    }

    @Override
    public void logoutTreeLineSearch(String account) {
        userMapper.logoutTreeLineSearch(account);
    }

    @Override
    public void logoutTwoPointsSearch(String account) {
        userMapper.logoutTwoPointsSearch(account);
    }
}
